package cn.iocoder.yudao.module.common.service.grade;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.grade.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.grade.GradeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.grade.GradeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 年级 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GradeServiceImpl implements GradeService {

    @Resource
    private GradeMapper gradeMapper;

    @Override
    public Long createGrade(GradeSaveReqVO createReqVO) {
        // 插入
        GradeDO grade = BeanUtils.toBean(createReqVO, GradeDO.class);
        gradeMapper.insert(grade);
        // 返回
        return grade.getId();
    }

    @Override
    public void updateGrade(GradeSaveReqVO updateReqVO) {
        // 校验存在
        validateGradeExists(updateReqVO.getId());
        // 更新
        GradeDO updateObj = BeanUtils.toBean(updateReqVO, GradeDO.class);
        gradeMapper.updateById(updateObj);
    }

    @Override
    public void deleteGrade(Long id) {
        // 校验存在
        validateGradeExists(id);
        // 删除
        gradeMapper.deleteById(id);
    }

    private void validateGradeExists(Long id) {
        if (gradeMapper.selectById(id) == null) {
            throw exception(GRADE_NOT_EXISTS);
        }
    }

    @Override
    public GradeDO getGrade(Long id) {
        return gradeMapper.selectById(id);
    }

    @Override
    public PageResult<GradeDO> getGradePage(GradePageReqVO pageReqVO) {
        return gradeMapper.selectPage(pageReqVO);
    }

}